﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.PKS;

namespace PKS.UserInterfaceHandler
{
    public class UserInterfaceReceiverHandler : SPFeatureReceiver
    {
        #region Member Variables
        private static readonly string loggerSource = "PKS ActivateHandler";
        private Logger logger = Logger.CreateDefaultLogger(loggerSource);
        #endregion

        #region Feature Activated Override

        /// <summary>
        /// Called when the feature is activated. Adds item handlers
        /// to all lists that support the PSMS Rated Item content type
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // Assume no errors
            string errorMessage = string.Empty;

            // Make sure we have something passed in for the properties
            if (properties != null && properties.Feature != null && properties.Feature.Parent != null)
            {
                // Get an instance of WebPartPages
                WebPartPages pages = new WebPartPages(properties, logger);

                // Create all of the web part pages
                pages.CreatePages();
            }
            else
            {
                // See what went wrong and set the error message appropriately
                if (properties == null)
                {
                    errorMessage = "properties parameter was passed in as null";
                }
                else if (properties.Feature == null)
                {
                    errorMessage = "properties.Feature was passed in as null";
                }
                else
                {
                    errorMessage = "properties.Feature.Parent was passed in as null";
                }
            }

            // See if there was an error.
            // If so log it.
            if (errorMessage.Length > 0)
            {
                logger.Write(EventLogEntryType.Error, errorMessage);
            }
        }


        /// <summary>
        /// Removes the pages installed by the feature from the Pages folder
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            string errorMessage = string.Empty;

            // Make sure we have something passed in for the properties
            if (properties != null && properties.Feature != null && properties.Feature.Parent != null)
            {
            }
            else
            {
                // See what went wrong and set the error message appropriately
                if (properties == null)
                {
                    errorMessage = "properties parameter was passed in as null";
                }
                else if (properties.Feature == null)
                {
                    errorMessage = "properties.Feature was passed in as null";
                }
                else
                {
                    errorMessage = "properties.Feature.Parent was passed in as null";
                }
            }

            // See if there was an error.
            // If so log it.
            if (errorMessage.Length > 0)
            {
                logger.Write(EventLogEntryType.Error, errorMessage);
            }
        }

        #endregion

        #region Unimplemented Overrides
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }


        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new NotImplementedException();
        }
        #endregion
    }
}







